package com.luchao.back.service.impl;

import com.luchao.back.common.exception.CheckIntegralException;
import com.luchao.back.common.exception.OrderShoesNullPointException;
import com.luchao.back.common.constant.Execute;
import com.luchao.back.mapper.*;
import com.luchao.back.pojo.OrderShoes;
import com.luchao.back.pojo.Orders;
import com.luchao.back.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private OrderShoesMapper orderShoesMapper;

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private ShoesMapper shoesMapper;

    @Autowired
    private ShoeSizesMapper shoeSizesMapper;

    @Override
    public List<Orders> getOrdersByPage(int pageNum, int pageSize, Integer ouid, Integer ostate) {
        return ordersMapper.getOrdersByPage((pageNum - 1) * pageSize, pageSize, ouid, ostate);
    }

    @Override
    public long getOrdersCount(Integer ouid, Integer ostate) {
        return ordersMapper.getOrdersCount(ouid, ostate);
    }

    @Override
    @Transactional
    public boolean changeOstate(Integer oid, Integer ouid, Float ointegral, Integer ostate) {
        boolean flag = true;
        try {
            if (ostate == Execute.ORDER_PAY_SUCCESS) {
                // 订单支付后,用户积分增加
                usersMapper.updateUsersIntegral(ointegral, ouid);
                List<OrderShoes> orderShoesList = orderShoesMapper.getOrderShoesByOid(oid);
                if (orderShoesList != null && orderShoesList.size() > 0) {
                    // 商品销售记录累加
                    for (OrderShoes orderShoes : orderShoesList) {
                        shoesMapper.updateShoesTimessOld(orderShoes.getOsnum(), orderShoes.getO_sid());
                    }
                    // 商品库存减少
                    for (OrderShoes orderShoes : orderShoesList) {
                        shoeSizesMapper.updateShoesCount(-orderShoes.getOsnum(), orderShoes.getO_sid(), orderShoes.getOssize());
                    }
                } else {
                    throw new OrderShoesNullPointException();
                }
            } else if (ostate == Execute.ORDER_APPLYING_RETURN) {
                //申请退货时,用户积分减少
                if (usersMapper.checkUsersIntegral(ointegral, ouid) > 0) {
                    usersMapper.updateUsersIntegral(-ointegral, ouid);
                } else {
                    throw new CheckIntegralException();
                }
            } else if (ostate == Execute.ORDER_RETURNED) {
                List<OrderShoes> orderShoesList = orderShoesMapper.getOrderShoesByOid(oid);
                // 退货处理之后,商品库存和销售量同步修改
                if (orderShoesList != null && orderShoesList.size() > 0) {
                    // 商品销售记录减少
                    for (OrderShoes orderShoes : orderShoesList) {
                        shoesMapper.updateShoesTimessOld(-orderShoes.getOsnum(), orderShoes.getO_sid());
                    }
                    // 商品库存增加
                    for (OrderShoes orderShoes : orderShoesList) {
                        shoeSizesMapper.updateShoesCount(orderShoes.getOsnum(), orderShoes.getO_sid(), orderShoes.getOssize());
                    }
                } else {
                    throw new OrderShoesNullPointException();
                }
            }
            ordersMapper.changeOstate(oid, ostate);
            orderShoesMapper.changeOrderShoesStateByOid(oid, ostate);
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    @Override
    @Transactional
    public Orders addOrders(Orders orders) {
        Orders order = null;
        try {
            orders.setOnum(System.currentTimeMillis() + "");
            int i = ordersMapper.createOrders(orders);
            if (i > 0) {
                orderShoesMapper.addOrderShoes(orders.getOid(), orders.getOrderShoes());
                order = orders;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return order;
        }
    }

    @Override
    public Orders getOrdersDetaileById(int oid) {
        return ordersMapper.getOrdersDetaileById(oid);
    }

}
